<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('在线列表')"/>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<body marginwidth="0" marginheight="0">
<div class="layui-fluid">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" id="userForm">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input id="username" name="username" autocomplete="off"
                                   class="layui-input" type="text"
                                   placeholder="请输入用户名"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" id="searchBtn"
                                class="xy-btn xy-btn-primary icon-btn xy-btn-sm">
                            <i class="layui-icon">&#xe615;</i> 搜索
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="online-table" lay-filter="online-table"></table>
    </div>
</div>
</div>
</body>

<script type="text/html" id="online-bar">
    <a  class="xy-btn xy-btn-danger xy-btn-xs" lay-event="remove">踢人</a>
</script>

<script type="text/html" id="online-state">
    <span class="pear-text">
        在线
    </span>
</script>
<script>
    layui.use(['table', 'form', 'jquery','notice'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let notice = layui.notice;

        let MODULE_PATH = basePath+"/online/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '用户名', field: 'account', align: 'center', width: 100},
                {title: '账号', field: 'username', align: 'center'},
                {title: '状态', field: 'username', align: 'center',templet: '#online-state'},
                {title: '在线时长', field: 'onlineTime', align: 'center'},
                {title: '登录时间', field: 'lastTime', align: 'center'},
                {title: '操作', toolbar: '#online-bar', align: 'center', width: 100}
            ]
        ]

        table.render({
            elem: '#online-table',
            url: MODULE_PATH + 'list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: false
        });

        table.on('tool(online-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
        });

        form.on('submit(online-query)', function (data) {
            table.reload('online-table', {where: data.field})
            return false;
        });

        window.remove = function (obj) {
        layer.confirm('确定要踢出该用户？', {icon: 5, title: '提示', skin: 'layer-ext-myskin'}, function (index) {
                layer.close(index);
                let loading = layer.load(1);
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['userId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                            obj.del();
                        } else {
                            top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                        }
                    }
                })
            });
        }

        // 搜索
        $('#searchBtn').on('click', function () {
            table.reload('online-table',{
                where: getFormData('userForm')
            });
        });
    })
</script>
</html>